// 洗衣车模块
import http from '@/shopro/request/index'

const state = {
  cartList: [], //洗衣车列表
  checkCart: {}, //检测是否是洗衣车数据的对象
  allSelected: false, // 全选检测
  cartNum: 0, //洗衣车,涉及到刷新数据丢失，所以存了本地,
}


const getters = {
  cartList: state => state.cartList,
  checkCart: state => state.checkCart,
  allSelected: state => state.allSelected,
  cartNum: state => state.cartNum,
  // 洗衣车数量和总价
  totalCount: state => {
    let totalNum = 0;
    let totalPrice = 0;
    state.cartList.filter(item => {
      if (item.checked) {
        totalNum += 1;
        // if (uni.getStorageSync("indexa") == null) {
          totalPrice += item.goods_num * (item.sku_price ? item.sku_price.price : 0);
        // } else {
        //   if (totalNum == 1) {
        //     totalPrice = 1
        //   } else {
        //     totalPrice += item.goods_num * (item.sku_price ? item.sku_price.price : 0);
        //   }
        // }
      }
    })
    return {
      totalNum,
      totalPrice
    }
  },

  // 是否选择了商品
  isSel: state => {
    let isSel = false;
    state.cartList.map(item => {
      if (item.checked) {
        isSel = true
      }
    })
    return isSel
  },

  // 活动商品唯一选中可以结算
  isActivityPay() {
    let num = 0
    let activityNum = 0
    state.cartList.map(item => {
      if (item.checked) {
        num += 1
        if (item.cart_type === 'activity') {
          activityNum += 1
        }
      }
    })
    if ((num === 1 && activityNum === 1) || activityNum === 0) {
      return true
    }
    return false
  }

}


const mutations = {
  // cart数据获取变动。
  CART_LIST(state, payload) {
    state.cartList = payload;
    let cartListNumber = payload.length;
    if (Number(cartListNumber)) {
      uni.setTabBarBadge({
        index: 2,
        text: String(cartListNumber)
      })
    } else {
      uni.removeTabBarBadge({
        index: 2
      })
    }
    state.cartNum = cartListNumber;
  },

  // 切换全选。
  changeAllSellect(state) {
    state.allSelected = !state.allSelected;
  },
  // 全选设置
  getAllSellectCartList(state, flag) {
    state.cartList.map(item => {
      item.checked = flag
    })
  },
  // 全选检测
  checkCartList(state) {
    let all = true;
    state.cartList.map(item => {
      if (!item.checked) {
        all = false
      }
    })
    state.allSelected = all;
  },
  // 是否洗衣车数据检测
  getCheckCart(state) {
    let obj = {};
    state.cartList.map(item => {
      obj[item.goods_id] = {
        num: item.goods_num,
        cartOrderId: item.id
      };
    });
    state.checkCart = obj;
  },
}

const actions = {
  // 洗衣车数据（查）
  getCartList({
    commit
  },data = {
	fromLucky:0
  }) {
    return new Promise((resolve, reject) => {
		console.log('是否是抽奖过来的',data);
      http('cart.index').then(res => {
        if (res.code === 1) {
          let cartData = res.data;
		  console.log(cartData,'获取列表数据');
		  if(data.fromLucky == 1){
			let foundItem = cartData.find(item => item.goods_id === data.maxDiscountItem.id);
			console.log(foundItem,'匹配数据');
			if(foundItem){
				foundItem.goods.price = '1.00';
				foundItem.sku_price.price = '1.00';
			}
		  } 
          cartData.length && cartData.map(item => {
            item.checked = true;
          })
          commit('CART_LIST', cartData);
          commit('checkCartList');
          commit('getCheckCart')
        }

      }).catch(e => {
        reject(e)
      })
    })
  },
  // 添加到洗衣车（增）
  addCartGoods({
    dispatch
  }, data) {
    return new Promise((resolve, reject) => {
      http('cart.add', {
        goods_list: data.list,
        from: data.from
      }).then(res => {
        res.code === 1 && dispatch('getCartList',data);
        resolve(res)
      }).catch(e => {
        reject(e)
      })
    })
  },
  // 修改洗衣车商品数量（改）|| 删除洗衣车商品（删）
  changeCartList({
    commit,
    dispatch
  }, param) {
    return new Promise((resolve, reject) => {
      http('cart.edit', {
        cart_list: param.ids,
        value: param.goodsNum || null,
        act: param.art
      }).then(res => {
        if (param.art === 'delete' && res.code === 1) {
          dispatch('getCartList');
        }
        commit('checkCartList');
        resolve(res)
      }).catch(e => {
        reject(e)
      })
    })
  },
}

export default {
  state,
  mutations,
  actions,
  getters
}